/*
 * SelectCourseInterFrm.java
 *
 * Created on __DATE__, __TIME__
 */

package com.jakey.view;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import com.jakey.dao.CourseDao;
import com.jakey.dao.SelectionDao;
import com.jakey.model.Course;
import com.jakey.model.Selection;
import com.jakey.util.DbUtil;

import Message.Message;
import client.TheClient;
import gui.colors.PrimaryColor;
import user.User;

import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.Dimension;
import java.awt.Color;

/**
 *
 * @author __USER__
 */
public class SelectCourseInterFrm extends javax.swing.JInternalFrame {

	CourseDao courseDao = new CourseDao();
	SelectionDao selectionDao = new SelectionDao();
	private int capacity;
	private int numSelected;
	private int courseId = -1;
	private User user;

	/** Creates new form SelectCourseInterFrm */
	public SelectCourseInterFrm(User user2) {
		this.user = user2;
		// getContentPane().setBackground(new PrimaryColor());
		getContentPane().setBackground(new Color(240, 240, 240));
		setPreferredSize(new Dimension(700, 433));
		initComponents();
		this.setLocation(200, 50);
		this.fillTable(new Course());
	}

	private void fillTable(Course course) {
		DefaultTableModel dtm = (DefaultTableModel) courseTable.getModel();
		dtm.setRowCount(0);
		String tp = "SHOW_READERS";
		Course course1 = new Course();
		Message message = new Message(tp, course1);
		message.setCheckCode(6);

		client.TheClient client = new TheClient();
		Message received;
		try {
			received = client.sendAndReceive(message);
			Vector<Course> TbB = new Vector<Course>();
			TbB = received.getCsvecVector();
			for (int i = 0; i < TbB.size(); i++) {
				Course temp = new Course();
				Vector v = new Vector();
				temp = TbB.get(i);
				v.add(temp.getCourseId());
				v.add(temp.getCourseName());
				v.add(temp.getCourseTime());
				v.add(temp.getCourseTeacher());
				v.add(temp.getCapacity());
				v.add(temp.getNumSelected());
				dtm.addRow(v);

			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jScrollPane1 = new javax.swing.JScrollPane();
		courseTable = new javax.swing.JTable();
		jb_underFull = new javax.swing.JButton();
		jb_confirm = new javax.swing.JButton();

		setClosable(true);
		setIconifiable(true);
		setTitle("\u8bfe\u7a0b\u9009\u62e9");

		courseTable.setModel(new javax.swing.table.DefaultTableModel(new Object[][] {

		}, new String[] { "课程编号", "课程名称", "上课时间", "任课老师", "课程容量", "已选人数" }) {
			boolean[] canEdit = new boolean[] { false, false, false, true, true, false };

			public boolean isCellEditable(int rowIndex, int columnIndex) {
				return canEdit[columnIndex];
			}
		});
		courseTable.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mousePressed(java.awt.event.MouseEvent evt) {
				courseTableMousePressed(evt);
			}
		});
		jScrollPane1.setViewportView(courseTable);

		jb_underFull.setText("\u53ea\u663e\u793a\u672a\u9009\u6ee1\u8bfe\u7a0b");
		jb_underFull.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jb_underFullActionPerformed(evt);
			}
		});

		jb_confirm.setText("\u786e\u8ba4\u9009\u8bfe");
		jb_confirm.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jb_confirmActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		layout.setHorizontalGroup(layout.createParallelGroup(Alignment.LEADING)
				.addGroup(layout.createSequentialGroup().addGap(142).addComponent(jb_underFull)
						.addPreferredGap(ComponentPlacement.RELATED, 201, Short.MAX_VALUE).addComponent(jb_confirm).addGap(135))
				.addGroup(layout.createSequentialGroup().addContainerGap()
						.addComponent(jScrollPane1, GroupLayout.DEFAULT_SIZE, 578, Short.MAX_VALUE).addContainerGap()));
		layout.setVerticalGroup(layout.createParallelGroup(Alignment.LEADING).addGroup(Alignment.TRAILING,
				layout.createSequentialGroup().addContainerGap()
						.addComponent(jScrollPane1, GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE).addGap(18)
						.addGroup(
								layout.createParallelGroup(Alignment.BASELINE).addComponent(jb_underFull).addComponent(jb_confirm))
						.addGap(33)));
		getContentPane().setLayout(layout);

		pack();
	}// </editor-fold>
		// GEN-END:initComponents

	private void jb_underFullActionPerformed(java.awt.event.ActionEvent evt) {
		DefaultTableModel dtm = (DefaultTableModel) courseTable.getModel();
		dtm.setRowCount(0);
		String tp = "UNDERFULL_LIST";
		Message message = new Message();
		message.setCheckCode(6);
		message.setType(tp);
		client.TheClient client = new TheClient();
		Message received;
		try {
			received = client.sendAndReceive(message);
			Vector<Course> TbB = new Vector<Course>();
			TbB = received.getCsvecVector();
			for (int i = 0; i < TbB.size(); i++) {
				Course temp = new Course();
				Vector v = new Vector();
				temp = TbB.get(i);
				v.add(temp.getCourseId());
				v.add(temp.getCourseName());
				v.add(temp.getCourseTime());
				v.add(temp.getCourseTeacher());
				v.add(temp.getCapacity());
				v.add(temp.getNumSelected());
				dtm.addRow(v);

			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private void courseTableMousePressed(java.awt.event.MouseEvent evt) {
		// int row = courseTable.getSelectedRow();
		//
		// courseId = Integer.parseInt((String) courseTable.getValueAt(row, 0));
		// capacity = Integer.parseInt((String) courseTable.getValueAt(row, 4));
		// numSelected = Integer.parseInt((String) courseTable.getValueAt(row, 5));

		int count = courseTable.getSelectedRow();
		String a = courseTable.getValueAt(count, 0).toString();
		String b = courseTable.getValueAt(count, 1).toString();
		String c = courseTable.getValueAt(count, 2).toString();
		String d = courseTable.getValueAt(count, 3).toString();
		String e = courseTable.getValueAt(count, 4).toString();
		String f = courseTable.getValueAt(count, 5).toString();
		int courseId = Integer.parseInt(a);
	}

	private void jb_confirmActionPerformed(java.awt.event.ActionEvent evt) {
		int count = courseTable.getSelectedRow();
		String a = courseTable.getValueAt(count, 0).toString();
		String b = courseTable.getValueAt(count, 1).toString();
		String c = courseTable.getValueAt(count, 2).toString();
		String d = courseTable.getValueAt(count, 3).toString();
		String ee = courseTable.getValueAt(count, 4).toString();
		String f = courseTable.getValueAt(count, 5).toString();
		int courseId = Integer.parseInt(a);
		if (courseId == -1) {
			JOptionPane.showMessageDialog(this, "请选择一门课程！");
			return;
		}
		if (ee == f) {
			JOptionPane.showMessageDialog(this, "该课程已选满,请选择其他课程.");
			return;
		}

		int n = JOptionPane.showConfirmDialog(this, "确定要选择该门课程吗?");
		if (n == 0) {

			int currentSno = Integer.valueOf(user.card).intValue();
			Selection selection = new Selection(courseId, currentSno);

			Message message = new Message();
			message.setAllembracing(selection);
			message.setType("SELECTION_ADD");
			message.setCheckCode(6);
			client.TheClient client = new TheClient();

			Message recevied = new Message();
			try {
				recevied = client.sendAndReceive(message);
				if (recevied.isResponse()) {
					JOptionPane.showMessageDialog(this, "选课成功!");
					this.fillTable(new Course());
				} else {
					JOptionPane.showMessageDialog(this, "选课成功!");
				}
			} catch (Exception e) {
				e.printStackTrace();
				JOptionPane.showMessageDialog(this, "已选过该门课程!");
			} finally {
				try {

				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}

	// GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JTable courseTable;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JButton jb_confirm;
	private javax.swing.JButton jb_underFull;
	// End of variables declaration//GEN-END:variables

}